#include "AppointmentDairy.h"
#include <iostream>
#include <sstream>

int chooseDay() {
	int day;
	for (day = 0 ; day < Day::days_in_week ; day++) {
		cout << "	" << day << ": " << Day::days_of_week[day] << endl;
	}
	cout << "	" << day << ": cancel"	<< endl;
	while (cout << "Choose the day : " && !(cin >> day))
	{
		cin.sync();
		cin.clear();
		cout << "Invalid input; please re-enter.\n";
	}

	return day;
}

int main() {

	AppointmentDairy * diary = new AppointmentDairy();
	bool cont	= true;			// trigger to stop loop
	unsigned int x,day, day2;
	char s[200];
	int startH, endH, startM, endM, startH2, startM2;
	string hStr, mStr;
	Appointment * app = NULL;

	cout << " Welcome to your appointment diary! " << endl
		 << " ---------------------------------- " << endl << endl;

	while (cont) {
		cout << endl << " What would you like to do? " 	<< endl
			 << "	0: insert " 			<< endl
			 << "	1: remove " 			<< endl
			 << "	2: find " 				<< endl
			 << "	3: copy" 				<< endl
			 << "	4: print"				<< endl
			 << "	5: create new diary" 	<< endl
			 << "	9: quit"				<< endl << endl;

		while (cout << "Enter your choice : " && !(cin >> x))
		{
			cin.sync();
			cin.clear();
			cout << "Invalid input; please re-enter.\n";
		}

		day = 99;
		day2 = 99;

		switch (x) {
			case 0: 
				{
					while (day < 0 || day > Day::days_in_week)
						day = chooseDay();
					if (day == Day::days_in_week) break;
					cout << "Starts at (HH:MM): ";
					getline(cin,hStr,':');
					getline(cin,mStr,'\n');
					stringstream(hStr) >> startH;
					stringstream(mStr) >> startM;
					cout << "Ends at (HH:MM): ";
					getline(cin,hStr,':');
					getline(cin,mStr,'\n');
					stringstream(hStr) >> endH;
					stringstream(mStr) >> endM;
					cout << "Subject: ";
					cin.getline(s,200);
					try {
						diary->addAppointment(day,startH,startM,endH,endM,s);
						cout << "Appointment added!" << endl;
					} catch (const ADException & ex) {
						cout << ex.what() << endl;
					}
					break;
				}
			case 1: {
					while (day < 0 || day > Day::days_in_week)
						day = chooseDay();
					if (day == Day::days_in_week) break;
					cout << "Starts at (HH:MM): ";
					getline(cin,hStr,':');
					getline(cin,mStr,'\n');
					stringstream(hStr) >> startH;
					stringstream(mStr) >> startM;
					app = const_cast<Appointment *> (diary->find(day,startH,startM));
					if (app == NULL)
						cout << "Appointment not found!" << endl;
					else {
						diary->removeAppointment(*app);
						cout << "Appointment removed!" << endl;
					}
					break;
					}
			case 2:{
					while (day < 0 || day > Day::days_in_week)
						day = chooseDay();
					if (day == Day::days_in_week) break;
					cout << "Starts at (HH:MM): ";
					getline(cin,hStr,':');
					getline(cin,mStr,'\n');
					stringstream(hStr) >> startH;
					stringstream(mStr) >> startM;
					app = const_cast<Appointment *> (diary->find(day,startH,startM));
					if (app == NULL)
						cout << "Appointment not found!" << endl;
					else
						cout << "Appointment found:\n" << *app << endl;
					break;
					}
			case 3: {
					cout << "Copy From: " << endl;
					while (day < 0 || day > Day::days_in_week)
						day = chooseDay();
					if (day == Day::days_in_week) break;
					cout << "Starts at (HH:MM): ";
					getline(cin,hStr,':');
					getline(cin,mStr,'\n');
					stringstream(hStr) >> startH;
					stringstream(mStr) >> startM;
					cout << "Copy To: " << endl;
					while (day2 < 0 || day2 > 7)
						day2 = chooseDay();
					if (day2 == 7) break;
					cout << "Starts at (HH:MM): ";
					getline(cin,hStr,':');
					getline(cin,mStr,'\n');
					stringstream(hStr) >> startH2;
					stringstream(mStr) >> startM2;
					app = const_cast<Appointment *> (diary->find(day,startH,startM));
					if (app == NULL)
						cout << "Appointment not found!" << endl;
					else {
						try {
							diary->copyAppointment(*app,day2,startH2,startM2);
							cout << "Appointment coppied!" << endl;
						} catch (const ADException & ex) {
							cout << ex.what() << endl;
						}
					}
					break;
					}
			case 4: {
					cout << *diary;
					break;
					}
			case 5: {
					delete diary;
					diary = new AppointmentDairy();
					cout << "New diary created, old diary removed!" << endl;
					break;
					}
			case 9: cont = false;
			default : break;
		}
	}

	return 0;
}